package com.example.tys.daggermvp.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.tys.daggermvp.Application.GlideApp;
import com.example.tys.daggermvp.R;
import com.example.tys.daggermvp.bean.ImageBean;
import com.example.tys.daggermvp.view.recyclerviewCompant.BaseViewHolder;
import com.example.tys.daggermvp.view.recyclerviewCompant.sticky.StickyBaseAdapter;

import java.util.ArrayList;

/**
 * @author tys
 * @date 2018/4/29
 */

public class ImagePickerAdapter extends StickyBaseAdapter<ImageBean, ImagePickerAdapter.ViewHolder> {

    public ImagePickerAdapter(Context context, ArrayList<ImageBean> mData) {
        super(context, mData);
    }

    public ImagePickerAdapter(Context context, ArrayList<ImageBean> mData, int spanCount) {
        super(context, mData, spanCount);
    }


    @Override
    public int getItemViewType(int position) {
        ImageBean item = mData.get(position);
        return item.isFilled() ? 1 : super.getItemViewType(position);
    }

    @Override
    public ImagePickerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView;
        if (viewType == 0) {
            itemView = LayoutInflater.from(context).inflate(R.layout.image_picker_item, null, false);
        } else {
            itemView = LayoutInflater.from(context).inflate(R.layout.image_picker_item, null, false);
        }
        return new ImagePickerAdapter.ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ImagePickerAdapter.ViewHolder holder, int position) {
        ImageBean item = mData.get(position);
        if(item.isFilled()){
            GlideApp.with(context)
                    .load(R.drawable.ic_launcher_background)
                    .centerCrop()
                    .into(holder.imageView);
        }else {
            GlideApp.with(context)
                    .load(item.getFilePath())
                    .centerCrop()
                    .error(R.mipmap.default_error)
                    .into(holder.imageView);
        }
    }


    class ViewHolder extends BaseViewHolder {
        private final ImageView imageView;

        ViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image_picker_item);
            setItemClickListen(itemView, ImagePickerAdapter.this);
        }
    }


}
